package com.sh.data.engine.application.model.request.api;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.List;

@Data
@Schema
public class DataApiRegisterRequest extends DataApiBaseRequest {


    @NotNull
    @Pattern(regexp = "^(GET|POST)$", message = "请求方式必须为GET或POST")
    @Schema(description = "请求方式", example = "GET POST", requiredMode = Schema.RequiredMode.REQUIRED)
    private String httpRequestMethod;

    // http url
    @NotNull
    @Schema(description = "路径", requiredMode = Schema.RequiredMode.REQUIRED)
    @Pattern(
        regexp =
            "^https?://.*",
        message = "url非法")
    private String url;

    // 认证参数
    @Valid
    @NotNull
    @Schema(description = "请求认证", requiredMode = Schema.RequiredMode.REQUIRED)
    private ApiHttpAuthParam apiHttpAuth;

    // 请求头
    @Valid
    @NotNull
    @Schema(description = "请求头")
    private ApiHttpRequestHeaderParam apiHttpRequestHeader;

    // 请求体
    @Valid
    @NotNull
    @Schema(description = "请求体")
    private ApiHttpRequestBodyParam apiHttpRequestBody;


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiHttpAuthParam {
        // 认证方式    noauth, basic, sign, token
        @Schema(description = "认证方式", example = "noauth, basic, sign, token, jar", requiredMode = Schema.RequiredMode.REQUIRED)
        @NotNull
        private String authType;

        // basic
        @Schema(description = "basic认证")
        private String authUsername;

        @Schema(description = "basic认证")
        private String authPassword;

        // sign
        @Schema(description = "sign")
        private String accessKey;

        @Schema(description = "sign")
        private String accessSecret;

        // token
        @Schema(description = "token")
        private String accessToken;

        @Schema(description = "jar缓存key")
        private String advancedCacheKey;

        // 资源名字
        @Schema(description = "jar 文件名")
        private String advancedFileName;
        // 资源路径
        @Schema(description = "jar 自定义认证")
        private String advancedFilePath;

        @Schema(description = "jar包class")
        private String advanceClassReference;

        @Schema(description = "jar包方法名")
        private String advanceMethodName;

        // 变量有效期
        @Schema(description = "有效期")
        private Date expires;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiHttpRequestHeaderParam {
        // 请求头列表
        @Schema(description = "请求头列表")
        private List<Header> params;

        @Data
        @Schema(description = "请求头")
        @NoArgsConstructor
        @AllArgsConstructor
        public static class Header {
            // 消息头名称
            @NotNull
            @Schema(description = "header name", example = "x-id", requiredMode = Schema.RequiredMode.REQUIRED)
            private String name;
            // 具体值
            @NotNull
            @Schema(description = "header value", example = "123", requiredMode = Schema.RequiredMode.REQUIRED)
            private String value;
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ApiHttpRequestBodyParam {

        @NotNull
        @Schema(description = "请求体传参方式", example = "json or form", requiredMode = Schema.RequiredMode.REQUIRED)
        private String contentType;
        // 请求体
        @Schema(description = "json")
        private String body;

        // url参数
        @Schema(description = "form参数")
        private List<FormParam> formParams;

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class FormParam {

            /**
             * 参数类型 0-输入参数 1-返回参数
             */
            @Schema(description = "参数类型", requiredMode = Schema.RequiredMode.REQUIRED)
            @NotNull
            private Integer paramType;

            /**
             * 参数名称
             */
            @NotNull
            @Schema(description = "参数名称", requiredMode = Schema.RequiredMode.REQUIRED)
            private String paramName;

            /**
             * 参数位置 query head body
             */
            @NotNull
            @Schema(description = "参数位置", example = "query", requiredMode = Schema.RequiredMode.REQUIRED)
            private String paramPosition = "query";

            /**
             * 参数数据类型
             */
            @NotNull
            @Schema(description = "参数数据类型", example = "STRING", requiredMode = Schema.RequiredMode.REQUIRED)
            private String paramDataType;

            /**
             * 参数值
             */
            @NotNull
            @Schema(description = "参数值", example = "123", requiredMode = Schema.RequiredMode.REQUIRED)
            private String paramValue;

            /**
             * 描述
             */
            @Schema(description = "参数描述")
            private String paramDesc;

            /**
             * 返回参数取值表达式
             */
            @Schema(description = "参数描述")
            private String paramExpression;

            /**
             * 参数属性 0-无(纯输入值) 1-pageNo 2-pageSize
             */
            @Schema(description = "分页参数默认填写0", requiredMode = Schema.RequiredMode.REQUIRED)
            @NotNull
            private Integer paramAttribute = 0;
        }
    }
}
